﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LIM.controller;

namespace LIM.forms.uc_CRM_HopDong_ChiTiet
{
    public partial class uc_CRM_HopDong_ChiTiet_ThongTinBenNhan : UserControl
    {
        public uc_CRM_HopDong_ChiTiet_ThongTinBenNhan()
        {
            InitializeComponent();
        }

        private void uc_CRM_HopDong_ChiTiet_ThongTinBenNhan_Load(object sender, EventArgs e)
        {
            VNThanhLoad();
        }

        //\\VNThanh
        void VNThanhLoad()
        {
            this.radMenuItem1.Click += new EventHandler(radMenuItem1_Click);

            this.radButton1.Click += new EventHandler(radButton1_Click);
            this.radListView1.SelectedIndexChanged += new EventHandler(radListView1_SelectedIndexChanged);
        }

        public int RowID { get; set; }
        public int RowIDMaKhachHang { get; set; }
        public Thanh_KhachHang Thanh_KhachHang { get; set; }
        public Thanh_HopDong Thanh_HopDong { get; set; }
        public int RowIDLienHe { get; set; }
        private LIM.controller.Thanh_CRMHopDongChiTietController _Controller;
        public LIM.controller.Thanh_CRMHopDongChiTietController Controller
        {
            get { if (_Controller == null) _Controller = new Thanh_CRMHopDongChiTietController(); return _Controller; }
            set { _Controller = value; }
        }

        // Lay gia tri cua cac control de luu.
        public Thanh_HopDong GetControlValues()
        {
            Thanh_HopDong result = new Thanh_HopDong();
            result.RowID = RowID;
            result.RowIDKhachHang = RowIDMaKhachHang;
            result.TenKhachHang = this.radTxtTenKH.Text;
            result.DiaChi = this.radTxtDiaChi.Text;
            result.SoDienThoai = this.radTxtSoDienThoai.Text;
            result.SoFax = this.radTxtSoFax.Text;

            result.RowIDLienHe = RowIDLienHe;
            result.HoTenNguoiDaiDien = this.radTxtNguoiNhan.Text;
            result.MaSoThue = this.radTxtMaSoThue.Text;
            result.TaiKhoanNganHang = this.radTxtTaiKhoanNganHang.Text;
            result.ChucVu = this.radTxtChucVu.Text;

            return result;
        }

        // Reset lai cac control lien quan ComboBox Khach Hang.
        public void SetControlValues(Thanh_KhachHang pThanh_KhachHang)
        {
            Thanh_KhachHang = pThanh_KhachHang;
            if (pThanh_KhachHang != null)
            {
                RowIDMaKhachHang = pThanh_KhachHang.RowID;
                this.radTxtTenKH.Text = pThanh_KhachHang.TenGoi;
                this.radTxtDiaChi.Text = pThanh_KhachHang.DiaChi;
                this.radTxtSoDienThoai.Text = pThanh_KhachHang.SoDienThoai;
                this.radTxtSoFax.Text = pThanh_KhachHang.SoFax;

                this.radListView1.DisplayMember = "HoTen";
                this.radListView1.ValueMember = "RowID";
                this.radListView1.DataSource = Controller.GetsLienHe(pThanh_KhachHang.RowID);
            }
            else
            {
                RowIDMaKhachHang = default(int);
                this.radTxtTenKH.Text =
                this.radTxtDiaChi.Text =
                this.radTxtSoDienThoai.Text =
                this.radTxtSoFax.Text = string.Empty;

                this.radListView1.DisplayMember = "HoTen";
                this.radListView1.ValueMember = "RowID";
                this.radListView1.DataSource = null;
            }
        }

        // Set cac control cua HopDong khi open.
        public void SetControlValues(Thanh_HopDong pThanh_HopDong)
        {
            if (pThanh_HopDong != null)
            {
                Thanh_HopDong = pThanh_HopDong;
                RowID = pThanh_HopDong.RowID;
                RowIDMaKhachHang = pThanh_HopDong.RowIDKhachHang;
                this.radTxtTenKH.Text = pThanh_HopDong.TenKhachHang;
                this.radTxtDiaChi.Text = pThanh_HopDong.DiaChi;
                this.radTxtSoDienThoai.Text = pThanh_HopDong.SoDienThoai;
                this.radTxtSoFax.Text = pThanh_HopDong.SoFax;

                this.radListView1.SelectedIndex = -1;
                foreach (var item in this.radListView1.Items)
                {
                    if (item.Value.ToString() == pThanh_HopDong.RowIDLienHe.ToString())
                    {
                        this.radListView1.SelectedItem = item;
                        RowIDLienHe = pThanh_HopDong.RowIDLienHe;
                        break;
                    }
                }
                this.radTxtNguoiNhan.Text = pThanh_HopDong.HoTenNguoiDaiDien;
                this.radTxtMaSoThue.Text = pThanh_HopDong.MaSoThue;
                this.radTxtTaiKhoanNganHang.Text = pThanh_HopDong.TaiKhoanNganHang;
                this.radTxtChucVu.Text = pThanh_HopDong.ChucVu;
            }
        }

        public void RefreshControl()
        {
            SetControlValues(Thanh_KhachHang);
            SetControlValues(Thanh_HopDong);
        }
        // Refresh.
        void radMenuItem1_Click(object sender, EventArgs e)
        {
            RefreshControl();
        }

        // An hien danh sach nguoi nhan.
        private void radButton1_Click(object sender, EventArgs e)
        {
            this.radPanel1.Visible = !this.radPanel1.Visible;
        }

        // Khi chon lien he trong combobox.
        private void radListView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.radListView1.SelectedItem != null)
            {
                Thanh_LienHe oThanh_LienHe = this.radListView1.SelectedItem.DataBoundItem as Thanh_LienHe;
                if (oThanh_LienHe != null)
                {
                    RowIDLienHe = oThanh_LienHe.RowID;
                    radTxtNguoiNhan.Text = oThanh_LienHe.HoTen;
                    radTxtChucVu.Text = oThanh_LienHe.ChucVu;
                    //radTxtSoDienThoaiLH.Text = oThanh_LienHe.SoDiDong;
                    //radTxtEmailCaNhan.Text = oThanh_LienHe.EmailCaNhan;
                    //radTxtEmailCongTy.Text = oThanh_LienHe.EmailCongTy;
                }
            }
            this.radPanel1.Visible = false;
        }
    }
}
